import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/styles/index.dart';

class CommonInputWidget extends StatefulWidget {
  final String? hintText;
  final Widget? addonBefore;
  final Widget? addonAfter;
  final Size? size;
  final int? maxLength;
  final TextInputType? keyboardType;
  final Function(String value) getValue;
  final bool showBorder;
  final EdgeInsetsGeometry? padding;
  final String? defaultValue;
  final int maxLines;
  final Color? backgroundColor;
  const CommonInputWidget({super.key, this.hintText, this.addonBefore, this.addonAfter, this.size, required this.getValue, this.maxLength, this.keyboardType, this.showBorder = true, this.padding, this.defaultValue, this.maxLines = 1, this.backgroundColor});

  @override
  State<CommonInputWidget> createState() => _CommonInputWidgetState();
}

class _CommonInputWidgetState extends State<CommonInputWidget> {
  late TextEditingController controller;

  @override
  void initState() {
    super.initState();
    controller = TextEditingController(text: widget.defaultValue);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.size?.width ?? 590.w,
      height: widget.size?.height ?? 100.w,
      alignment: Alignment.center,
      padding: widget.padding ?? EdgeInsets.only(left: 30.w, right: 20.w),
      decoration: BoxDecoration(
        color: widget.backgroundColor ?? ColorStyle.themeLightColor,
        borderRadius: BorderRadius.all(Radius.circular(50.w)),
        border: widget.showBorder ? Border.all(width: 5.w, color: ColorStyle.borderColor) : null
      ),
      child: Row(
        children: [
          if (widget.addonBefore != null)
            widget.addonBefore!,
          Expanded(child: TextField(
            controller: controller,
            style: TextStyle(fontSize: 30.sp, color: ColorStyle.textColor),
            textAlignVertical: TextAlignVertical.center,
            keyboardType: widget.keyboardType ?? TextInputType.text,
            maxLines: widget.maxLines,
            decoration: InputDecoration(
                isCollapsed: true,
                border: InputBorder.none,
                contentPadding: EdgeInsets.only(top: 15.w, bottom: 15.w),
                hintText: widget.hintText,
                hintStyle: TextStyle(
                    fontSize: 30.sp,
                    color: ColorStyle.textColor
                )
            ),
            inputFormatters: <TextInputFormatter>[
              LengthLimitingTextInputFormatter(widget.maxLength)//限制长度
            ],
            cursorColor: ColorStyle.textColor,
            onChanged: (String v) {
              String tmpValue = v.trim();
              if (widget.maxLength != null && tmpValue.runes.length > widget.maxLength!) {
                tmpValue = tmpValue.substring(0, widget.maxLength);
              }
              widget.getValue(tmpValue);
            },
          )),
          if (widget.addonAfter != null)
            widget.addonAfter!,
        ],
      ),
    );
  }
}
